<?php
App::uses('AppModel', 'Model');
/**
 * User Model
 *
 */
class User extends AppModel {

/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'email';


    public $actsAs = array(
            'MeioUpload.MeioUpload' => array(
              'image' => array('dir' => 'files/user','length' => array(
			'minWidth' => 250, // 0 for not validates
			'maxWidth' => 1280,
			'minHeight' => 300,
			'maxHeight' => 1080
		),'thumbsizes' => array('thumb' => array('width' => 600, 'height' => 400))),

            'documents' => array('dir' => 'files/user_doc/',
			'allowedMime' => array('application/pdf','image/jpeg', 'image/pjpeg', 'image/gif', 'image/png'),
            'allowedExt' => array('.pdf','.jpg', '.jpeg', '.png', '.gif'),
			'length' => array(
			'minWidth' => 0, // 0 for not validates
			'maxWidth' => 0,
			'minHeight' => 0,
			'maxHeight' => 0
		)),
          ),
        );


/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'email' => array(
			'email' => array(
				'rule' => array('email'),
				'message' => 'Email is required',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'validEmailRule' => array(
				'rule' => array('email'),
				'message' => 'Invalid email address'
			),
			'uniqueEmailRule' => array(
				'rule' => 'isUnique',
				'message' => 'Email already registered'
			)
		),

    "first_name" => array(
            "rule"      => "alphaNumeric",
            "message"   => 'Only alpha Numeric',
    ),
    "last_name"  => array(
            "rule"      => "alphaNumeric",
            "message"   => 'Only alpha Numeric',
    ),


		'password' => array(
            'rule' => array('minLength', '5'),
            'message' => 'Minimum 5 characters long'
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		'password' => array(
            'rule' => array('minLength', '5'),
            'message' => 'Minimum 5 characters long',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				'on' => 'create' // Limit validation to 'create' or 'update' operations
			),
		'new_password' => array(
            'rule' => array('minLength', '5'),
            'message' => 'Minimum 5 characters long',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		're_password' => array(
			'compare'    => array(
				'rule'      => array('validate_passwords'),
				'message' => 'The passwords you entered do not match.',
				'allowEmpty' => true,
			)
			),
		'facebook' => array(
			'url' => array(
				'rule' => array('url'),
				'message' => 'Invalid URL',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'twitter' => array(
			'url' => array(
				'rule' => array('url'),
				'message' => 'Invalid URL',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'google_plus' => array(
			'url' => array(
				'rule' => array('url'),
				'message' => 'Invalid URL',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'linkedin' => array(
			'url' => array(
				'rule' => array('url'),
				'message' => 'Invalid URL',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
        'user_role' => array(
            'valid' => array(
                'rule' => array('inList', array('agent','broker_admin','broker_agent')),
                'message' => 'Please select a valid Type',
                'allowEmpty' => false
            )
        ),
	);
			public function validate_passwords() {

			if(isset($this->data[$this->alias]['password']) and ($this->data[$this->alias]['password'] === $this->data[$this->alias]['re_password']))
			return true;
			else if(isset($this->data[$this->alias]['new_password']) and ($this->data[$this->alias]['new_password'] === $this->data[$this->alias]['re_password']))
			return true;
			else
			return false;
		}		

			
			
			public function beforeSave($options = array()) {

                    if(isset($this->data[$this->alias]['re_password']) and !empty($this->data[$this->alias]['re_password'])) {
                        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['re_password']);
                    }
                    if(isset($this->data[$this->alias]['password']) and !empty($this->data[$this->alias]['password'])) {
                        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
                    }
                    if(isset($this->data[$this->alias]['new_password']) and !empty($this->data[$this->alias]['new_password'])) {
                        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['new_password']);
                    }

                return true;
	}




}
